<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Nav;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class NavController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $items = Nav::latest('orderby')->get();
        return $this->successful($items);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $info = Nav::where('name', $request->post('name'))->exists();
        if ($info) {
            Cache::forget('cache:navs');
            return $this->successful();
        }
        return $this->successful(Nav::create($request->all()));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Nav  $nav
     * @return \Illuminate\Http\Response
     */
    public function show(Nav $nav)
    {
        return $this->successful($nav);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  \App\Models\Nav  $nav
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Nav $nav)
    {
        $nav->update($request->all());

        return $this->successful($nav);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Nav  $tag
     * @return \Illuminate\Http\Response
     */
    public function destroy(Nav $nav)
    {
        $nav->delete();
        return $this->successful([], '删除成功');
    }

    public function batchDelete()
    {
        $ids = request()->post('id');
        if ($ids) {
            Nav::destroy($ids);
        }
        return $this->successful($ids, '删除成功');
    }
}
